package com.cvr.project.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.ActivityUtils;
import com.cvr.project.R;
import com.cvr.project.api.ApiService;
import com.cvr.project.utils.DomainManager;
import com.cvr.project.utils.WatermarkManager;
import com.gyf.immersionbar.ImmersionBar;

/**
 * _ooOoo_
 * o8888888o
 * 88" . "88
 * (| -_- |)
 * O\ = /O
 * ____/`---'\____
 * .   ' \\| |// `.
 * / \\||| : |||// \
 * / _||||| -:- |||||- \
 * | | \\\ - /// | |
 * | \_| ''\---/'' | |
 * \ .-\__ `-` ___/-. /
 * ___`. .' /--.--\ `. . __
 * ."" '< `.___\_<|>_/___.' >'"".
 * | | : `- \`.;`\ _ /`;.`/ - ` : | |
 * \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 * `=---='
 * .............................................
 * 佛祖保佑             永无BUG
 *
 * @author :created by master
 * time :2024/12/27
 * 描述：
 */
public class WebViewActivity extends AppCompatActivity {
    public static void getInstance(Context context, String url) {
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra("title", "自助服务");
        intent.putExtra("url", DomainManager.getStaticH5Url() + "?token=" + url);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        ImmersionBar.with(this).transparentStatusBar().titleBar(R.id.app_title).navigationBarColor(R.color.white).statusBarDarkFont(true).init();
        

        
        initView();
        initListener();
    }

    private WebView webView;
    private ProgressBar progressbar;
    private TextView tvTitle;

    @SuppressLint("SetJavaScriptEnabled")
    private void initView() {
        webView = findViewById(R.id.webView);
        progressbar = findViewById(R.id.progressbar);
        String title = getIntent().getStringExtra("title");
        String url = getIntent().getStringExtra("url");
        findViewById(R.id.iv_home).setVisibility(View.VISIBLE);
        findViewById(R.id.iv_home).setOnClickListener(v -> {
            if (!(ActivityUtils.getTopActivity() instanceof HomeActivity)) {
                // 启动HomeActivity使用singleTop模式
                Intent intent = new Intent(this, HomeActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                finish();
            }
        });
        tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText(title);
        WebSettings mWebSettings = webView.getSettings();
        mWebSettings.setJavaScriptEnabled(true);
        mWebSettings.setUseWideViewPort(true);
        mWebSettings.setLoadWithOverviewMode(true);
        mWebSettings.setDomStorageEnabled(true);
        mWebSettings.setBuiltInZoomControls(true);
        mWebSettings.setSupportZoom(true);
        mWebSettings.setDisplayZoomControls(true);
//        mWebSettings.setAppCacheEnabled(true);
        mWebSettings.setTextZoom(100);
        webView.loadUrl(url);
    }

    private void initListener() {
        findViewById(R.id.iv_back).setOnClickListener(v -> {
            if (webView.canGoBack()) {
                webView.goBack();
            } else {
                finish();
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (null != progressbar) {
                    if (newProgress == 100) {
                        progressbar.setVisibility(View.GONE);
                    } else {
                        progressbar.setVisibility(View.VISIBLE);
                        progressbar.setProgress(newProgress);
                    }
                }
            }
        });
        webView.setWebViewClient(new WebViewClient() {
            @SuppressLint({"SetJavaScriptEnabled", "WebViewClientOnReceivedSslError"})
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
                if (null != webView) {
                    webView.getSettings().setJavaScriptEnabled(true);
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        
        // 添加水印
        WatermarkManager.getInstance().addWatermark(this);
    }
    
    @Override
    protected void onDestroy() {
        // 移除水印
        WatermarkManager.getInstance().removeWatermark(this);
        
        webView.getSettings().setJavaScriptEnabled(false);
        super.onDestroy();
    }
}
